import { inject, reactive } from 'vue'

function useSetAvatar(){
    const vueState = inject('vueState')
    const state = reactive({
        avatar:vueState.pageInfo.avatar || '',
        uploadAvatar:vueState.pageInfo.serverAvatar || '',
    })
    const setAvatar = ()=> {
        window.trackEvent({pageTitle:'profilePage',ev:'clickPicture'})
        window.AdSDK.addEventListener('ad_avatar_callback',setAvatarCallback)
        window.SDKPostMessage({
            event:'h5_open_page',
            data:{
                target:'avatar',
            }
        })
    }
    const setAvatarCallback = (res)=> {
        state.avatar = res.data.localUrl
        state.uploadAvatar = res.data.serverUrl
        console.log('setAvatarCallback',res);
        window.AdSDK.removeEventListener('ad_avatar_callback',setAvatarCallback)
    }
    return {
        setAvatar,
        state
    }
}
export {useSetAvatar}